package com.xiaoyu.awakening.user.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import com.xiaoyu.awakening.user.dto.AwkTokenDto;
import com.xiaoyu.awakening.user.dto.req.PasswdLoginReq;
import com.xiaoyu.awakening.user.dto.req.UserRegisterReq;
import com.xiaoyu.awakening.user.dto.res.CaptchaRes;
import com.xiaoyu.awakening.user.dto.res.PasswdLoginRes;
import com.xiaoyu.awakening.user.entity.UserEntity;
import com.xiaoyu.awakening.user.service.IUserLoginService;
import com.xiaoyu.awakening.user.service.IUserService;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 用户基本业务
 *
 * @author wangxiaoyu
 * @since v1.0
 */
@Log4j2
@Service
@AllArgsConstructor
public class UserLoginServiceImpl implements IUserLoginService {
    @Resource
    private IUserService userService;

    /**
     * 根据用户名密码登陆（简版）
     *
     * @param passwdLoginReq 登陆请求
     * @author wangxiaoyu
     * @since v1.0
     */
    @Override
    public PasswdLoginRes passwdLogin(PasswdLoginReq passwdLoginReq) {
        //todo:校验验证码
        //todo:校验用户锁定状态
        UserEntity user = userService.getUserByUserName(passwdLoginReq.getUsername());
        if (Objects.isNull(user) || !user.getPassword().equals(passwdLoginReq.getPassword())) {
            //       throw new GlobalException(UserErrorCodeEnums.LOGIN_CHECK_ERROR.getCode(), UserErrorCodeEnums.LOGIN_CHECK_ERROR.getMsg());
        }
        //执行登陆
        if (StpUtil.isLogin(user.getId())) {

        }
        StpUtil.login(user.getId());

        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        PasswdLoginRes passwdLoginRes = new PasswdLoginRes();
        AwkTokenDto awkTokenDto = new AwkTokenDto();
        BeanUtil.copyProperties(tokenInfo, awkTokenDto);
        passwdLoginRes.setAwkToken(awkTokenDto);
        return passwdLoginRes;
    }

    @Override
    public CaptchaRes getCaptcha() {
        String id = UUID.fastUUID().toString();
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 200);
        String imageBase64 = captcha.getImageBase64();
        String code = captcha.getCode().trim();
        //id+code+时间存入Redis
        return CaptchaRes.builder().captchaId(id).captchaImage(imageBase64).build();
    }

    /**
     * 用户注册
     *
     * @param userRegisterReq userRegisterReq
     * @author wangxiaoyu
     * @since v1.0
     */
    @Override
    public void register(UserRegisterReq userRegisterReq) {
        UserEntity existUser = userService.getUserByUserName(userRegisterReq.getName());
        if (Objects.nonNull(existUser)) {
            //     throw new GlobalException("用户名已经存在");
        }
        UserEntity UserEntity = new UserEntity();
        //   UserEntity.setName(userRegisterReq.getName());
        UserEntity.setPassword(userRegisterReq.getPassword());
        userService.save(UserEntity);
    }
}
